﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.IO;

namespace Vit.Web.Mvc.Results
{
    public class PartialHtmlView : PartialViewResult
    {
        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (string.IsNullOrEmpty(this.ViewName))
            {
                this.ViewName = context.RouteData.GetRequiredString("action");
            }
            ViewEngineResult result = null;
            if (this.View == null)
            {
                result = this.FindView(context);
                this.View = result.View;
            }
            ViewContext viewContext = new ViewContext(context, this.View, this.ViewData, this.TempData);
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            base.View.Render(viewContext, sw);
            sw.Flush();
            sw.Close();

            context.HttpContext.Response.Clear();
            context.HttpContext.Response.ContentType = "text/html";
            context.HttpContext.Response.Write(sb.ToString());
            if (result != null)
            {
                result.ViewEngine.ReleaseView(context, this.View);
            }
        }
    }
}
